﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing;

namespace FileExplorerTree
{
    public class FileExplorer
    {
        public FileExplorer()
        {
        }

        public void CreateTree(TreeView treeView)
        {
            try
            {
                foreach (DriveInfo drv in DriveInfo.GetDrives())
                {
                    TreeNode fChild = new TreeNode();
                    if (drv.DriveType == DriveType.CDRom)
                    {
                        fChild.ImageIndex = 1;
                        fChild.SelectedImageIndex = 1;
                    }
                    else
                        if (drv.DriveType == DriveType.Fixed)
                        {
                            fChild.ImageIndex = 0;
                            fChild.SelectedImageIndex = 0;
                        }
                    fChild.Text = drv.Name;
                    fChild.Nodes.Add("");
                    treeView.Nodes.Add(fChild);
                }
            }
            catch (Exception){}
        }
        
        public TreeNode EnumerateDirectory(TreeNode parentNode)
        {
            try
            {
                DirectoryInfo rootDir;
                Char [] arr={'\\'};
                string [] nameList=parentNode.FullPath.Split(arr);
                rootDir = new DirectoryInfo(parentNode.FullPath + "\\");
                parentNode.Nodes[0].Remove();
                foreach (DirectoryInfo dir in rootDir.GetDirectories())
                {                  
                    TreeNode node = new TreeNode();
                    node.Text = dir.Name;
                    node.Nodes.Add("");
                    parentNode.Nodes.Add(node);
                }
            
                foreach (FileInfo file in rootDir.GetFiles())
                {
                    TreeNode node = new TreeNode();
                    node.Text = file.Name;
                    node.ImageIndex = 2;
                    node.SelectedImageIndex = 2;
                    parentNode.Nodes.Add(node);
                }

            }
            catch (Exception){}
            return parentNode;
        }
    }
}
